/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.dbmapping.persistence;

import com.elcom.emocbc.dbmapping.tables.daos.BroadcasttimeDao;
import com.elcom.emocbc.dbmapping.tables.pojos.Broadcasttime;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.jooq.Configuration;
import org.jooq.ConnectionProvider;
import org.jooq.SQLDialect;
import org.jooq.conf.Settings;
import org.jooq.conf.StatementType;
import org.jooq.impl.DefaultConfiguration;

/**
 *
 * @author GiangLT
 */
public class BroadcasttimePersistance extends BroadcasttimeDao {

    public BroadcasttimePersistance() {
        ConnectionProvider connectionProvider = new DbConnectionProvider();
        Settings settings = new Settings();
        settings.setStatementType(StatementType.STATIC_STATEMENT);
        Configuration configuration = new DefaultConfiguration()
                .set(connectionProvider)
                .set(SQLDialect.ORACLE)
                .set(settings);
        this.setConfiguration(configuration);
    }

    public Broadcasttime findOneByZoneIdAndStartTime(Long zoneId, String startTime) throws SQLException {
        ResultSetHandler<Broadcasttime> resultSetHandler = new ResultSetHandler<Broadcasttime>() {
            @Override
            public Broadcasttime handle(ResultSet rs) throws SQLException {
                if (rs.next()) {
                    int i = 1;
                    Broadcasttime broadcasttime = new Broadcasttime();
                    broadcasttime.setBroadcasttimeId(BigInteger.valueOf(rs.getInt(i++)));
                    broadcasttime.setProgramIndexs(BigInteger.valueOf(rs.getInt(i++)));
                    broadcasttime.setCpTopicId(BigInteger.valueOf(rs.getInt(i++)));
                    broadcasttime.setZoneId(BigInteger.valueOf(rs.getInt(i++)));
                    broadcasttime.setStarttime(rs.getString(i++));
                    broadcasttime.setStatus(rs.getLong(i++));
                    broadcasttime.setCreatedby(BigInteger.valueOf(rs.getInt(i++)));
                    broadcasttime.setCreateddate(rs.getTimestamp(i++));

                    return broadcasttime;
                } else {
                    return null;
                }
            }
        };
        
        Connection con = DbAccess.getInstance().getConnection();
        QueryRunner qr = new QueryRunner();
        String sql = "SELECT * "
                + " FROM broadcasttime "
                + " WHERE zone_id = ? "
                + " AND starttime = ? ";
        try {
            return qr.query(con, sql, resultSetHandler, zoneId, startTime);
        } finally {
            DbUtils.closeQuietly(con);
        }
    }
}
